<?php
use yii\helpers\Url;
use app_supplier\assets\SuppAssets;
SuppAssets::addArrayCss($this, ['@web/styles/purchase.css','@web/css/page.css']);
SuppAssets::addArrayScript($this, ['@web/admin/view/javascript/jquery/ui/i18n/jquery-ui.js','@web/admin/view/javascript/jquery/ui/i18n/jquery-ui-slide.min.js','@web/admin/view/javascript/jquery/ui/i18n/jquery-ui-timepicker-addon.js','@web/javascript/jquery.selectseach.js','@web/javascript/jquery.freezeheader.js']);
use yii\widgets\LinkPager;
?>
<style>
    a.selected {
        color: #FD4A08;
    }

    ul {
        margin: 0px;
        border: 0px;
        padding: 0px;
    }

    .query-param {
        padding-left: 40px;
        min-height: 36px;
        position: relative;
    }

    .query-param span.title {
        display: inline-block;
        height: 36px;
        line-height: 36px;
        font-size: 13px;
        font-weight: bold;
        color: #000;
        width: 40px;
        text-align: left;
        position: absolute;
        left: 0px;
        top: 0px;
    }

    .query-param ul li { }
    ul li { list-style-type: none;}
    .selling_price { display: none }
    .pagination > li {
        margin: 0px;
    }
    #pagination{
        border-bottom: 1px #CCCCCC solid;
        position: relative;
    }
    .pagination{
        margin: 0px;
        width: inherit;
        position: absolute;
        right:0px;
    }
</style>
<script>
    $(document).ready(function () {
        $("#to_purchase").freezeHeader({ 'height': '400px' });
        $("#showStoreCategoryCb").prop({checked:false});
        $("#set_selling_price").prop({checked:false});
    })

    var index = 0;
    var total = 0;
    var issetPrint = parseInt("<?=$issetPrint?>");
    var app = {
        init: function () {
            $('.date').datetimepicker({ });
            app.change.loadBrand(0);  //初始化机油滤清器的品牌
        },
        change: {
            material: function (e) {
                var id = $(e).attr("data-id");
                var key = $(e).attr("data-key");
                $("." + key + "-item").removeClass("selected");
                $(e).addClass("selected");
                $("#" + key).val(id);
                this.loadBrand(id);

            },
            brand: function (e) {
                var key = $(e).attr("data-key");
                $("." + key + "-item").removeClass("selected");
                $(e).addClass("selected");
                var id = $("." + key + "-item.selected").attr("data-id");
                $("#" + key).val(id);
                app.search();
            },
            loadBrand: function (id) {
                var isChecked = $("#showStoreCategoryCb").is(':checked');
                var url = "<?=Url::to(['inventory/brand'])?>?id=" + id;
                if (isChecked){
                    url = "<?=Url::to(['inventory/brand'])?>?id=" + id+"&isStoreBrand=1"
                }
                $.post(url, { }, function (d) {
                    app.search();
                    var brandHtml = '';
                    brandHtml += "<li><a onclick=\"app.change.brand(this)\" href=\"javascript:void(0);\"  data-id=\"\" class=\"mt_select brand-item selected\" data-key=\"brand\" >全部</a></li>";
                    $(d).each(function (i, item) {
                        var name = item.brand;
                        brandHtml += "<li><a onclick=\"app.change.brand(this)\" href=\"javascript:void(0);\"  data-id=\"" + name + "\" class=\"mt_select brand-item \" data-key=\"brand\" >" + name + "</a></li>";
                    });
                    $("#brand-list").html(brandHtml);
                    $("#brand-list").parents(".query-param").show();
                }, "json")
            }
        },
        search: function (page) {
            page = page || 1;
            var pdata = { };
            $(".group-key").each(function (i, item) {
                var value = $(item).val();
                //if($.trim(value)!="")
                {
                    var name = $(item).attr("name");
                    pdata[name] = value;
                }
            });
            $("#to_select tbody").empty();
            var isChecked = $("#showStoreCategoryCb").is(':checked');
            var url = "<?=Url::to(['inventory/getallmaterialdata'])?>?page=" + page + "&count=15&isStoreMaterial=0";
            if (isChecked){
                url = "<?=Url::to(['inventory/getallmaterialdata'])?>?page=" + page + "&count=15&isStoreMaterial=1";
            }
            $.post(url, pdata, function (d) {
                $("#pagination").html(d.pages);
                $('.pagination li>a').each(function(){
                    var page_no = parseInt($(this).attr('data-page'))+1;
                    $(this).attr('href','javascript:app.page('+page_no+')')
                });
                $("#to_select tr:gt(0)").each(function () {
                    $(this).parent().remove();
                });
                var table = $("#to_select");
                var products = d.all;

                if (products.length > 0) {
                    for (var i = 0; i < (products.length); i++) {
                        var row = $("<tr></tr>");
                        var td = $("<td class='left'>" + products[i].name + "</td>" +
                            "<td class='left'>" + products[i].brand + "</td>" +
                            "<td class='left' title='" + products[i].description + "'>" + products[i].type + "</td>" +
                            "<td class='center'> <a class='add' href='javascript:void(0);' " +
                            "name='" + products[i].name + "_" + products[i].id + "_" + products[i].type + "_" + products[i].sale_price + "_" + products[i].material_id +
                            "_" + products[i].material_type_id + "_" + products[i].dealer_id + "_" + products[i].format + "' onclick='javascript:app.purchase.add(this);' " +
                            "data-brand='" + products[i].brand + "'></a> </td>");

                        $(row).data('data', products[i]);
                        row.append(td);
                        table.append(row);
                    }
                }
                else {
                    table.append("<tr><td class=\"center\" colspan=\"6\">没有产品信息</td></tr>");
                }
            }, "json")
        },
        submit: function () {
            var dealer_id = arguments[0] ? arguments[0] : '';
            var isChecked = $("#set_selling_price").is(':checked');
            var flag1 = false;
            var flag2 = false;
            var pdata = { items: [], total: 0, remark: '', date: '', dealer_id: dealer_id, borrow: 0, operator: '', operation: 0,choose_selling:0 }
            $("#to_purchase tbody tr").each(function (i, tr) {
                var price = $(tr).find('td').eq(3).find('input').val();
                var selling_price = $(tr).find('td').eq(4).find('input').val();
                var arr = $(tr).data('data');
                var item = { };
                if (price == '' && price != '0' ) {
                    flag1 = true;
                    return;
                }
                if (selling_price == '' && selling_price != '0' && isChecked) {
                    flag2 = true;
                }
                var mdata = arr.data;
                item['name'] = mdata.name;
                item['material_type'] = mdata.type;
                item['price'] = arr.price;
                item['selling_price'] = parseFloat(selling_price) ;
                item['material_id'] = mdata.mid;
                item['material_type_id'] = mdata.mtid;
                item['sku'] = mdata.format;
                item['quantity'] = arr.num;
                item['brand'] = mdata.brand;
                item['subtotal'] = arr.price * arr.num;
                pdata.items.push(item)
                pdata.total += arr.price * arr.num;
            });
            var worker = '';
            $("#workers li input[type=checkbox]").each(function () {
                if (this.checked) {
                    worker += $(this).val() + ',';
                }
            });
            worker = worker.substring(0, worker.length - 1);//去掉最后一个逗号
            pdata.operator = worker;
            pdata.operation = parseInt($("#operation").val());
            pdata.remark = $("#extra_requirement").val();
            pdata.date = $("#create_date").val();
            if ($("#set_selling_price").prop("checked")){
                pdata.choose_selling = 1;
            }
            if (flag1) {
                layer.alert("请填写材料的进价");
                return;
            }
            if (flag2 && isChecked) {
                layer.alert("请填写材料的售价");
                return;
            }
            if (pdata.items.length == 0) {
                layer.alert("请选择要入库的配件");
                return;
            }
            if (pdata.date == "") {
                layer.alert("请选择入库时间");
                $("#create_date").focus();
                return;
            }
            /*if (pdata.dealer_id == "" || pdata.dealer_id == "addDealer") {
             layer.msg("请选择供应商", {icon:2, time:1800});
             return;
             }*/
            return pdata;
        },
        clearing: function () {
            //判断是否调拨
            if($("#operation").val() == 7){
                if(!$('#choose_store').val()){
                    layer.alert('请选择调拨连锁门店！');
                    return;
                }
                var dealer_id = $('#choose_store').val();
                var is_allocate = 1;
            }else{
                var dealer_id = $('#choose_dealer').val();
                var is_allocate = 0;
            }
            var postData = app.submit(dealer_id);
            if (!postData) {
                return;
            }
            //加载层-默认风格
//            var layerIndex = layer.load();
            $("#btn-submit").attr("disabled","disabled");
            $.post("<?=Url::to(['inventory/warehousingenter'])?>", { postData:postData, is_allocate:is_allocate }, function (d) {
                if (d.flag) {
                    if(is_allocate == 0) {
                        var url = '<?=Url::to(['inventory/clearing'])?>?header_number=' + d.data['header_number'] + '&dealer_id=' + dealer_id;
                        layer.confirm("提交入库成功,是否结算？",{
                            btn:['结算','不结算'],
                            closeBtn: false,
                            shadeClose: false
                            },
                            function(index) {
                                layer.close(index);
                                layer.open({
                                    type: 2,
                                    title: "入库结算",
                                    skin: "layui-layer-lan",
                                    shadeClose: false,
                                    area: ['820px', '400px'],
                                    content: url,
                                    end: function () {
                                        window.location.reload();
                                    }
                                })
                            },
                            function (){
                                if (issetPrint =1) {
                                    layer.confirm('是否打印入库单',{
                                            btn:['打印','不打印'],
                                            closeBtn: false,
                                            shadeClose: false
                                        },
                                        function(index){
                                            layer.close(index);
                                            parent.location.reload();
                                            window.open("<?=Url::to(['inventory/print_warehouse'])?>?header_number=" + d.data['header_number']);
                                        },
                                        function () {
                                            parent.location.reload();
                                        });
                                } else {
                                    parent.location.reload();
                                }
                            });
                    }else if(is_allocate == 1)
                        layer.confirm('申请调拨成功，请等待审核！',{
                                closeBtn: 0,   //不显示右上角关闭按钮
                                title: '提示信息',
                                icon: 1,
                                btn:['知道了']},
                            function (index){ //确定时执行（第一个按钮）
                                layer.close(index);
                                window.location.reload();
                            });
                }
                else
                    layer.alert(d.msg);
            }, "json");
        },
        page: function (n) {
            this.search(n);
        },
        print:function (header_number){
            layer.confirm('结算成功，是否打印入库单？',{
                    btn:['打印','不打印'],
                    closeBtn: false,
                    shadeClose: false
                },
                function(index){
                    layer.close(index);
                    parent.window.open("<?=Url::to(['inventory/print_warehouse'])?>?header_number="+header_number);
                    parent.location.reload();
                },
                function () {
                    parent.location.reload();
                });
        },
        purchase: {
            add: function (e) {
                index++;
                var data = $(e).parents("tr").data('data');

                var exist = false;
                $("#to_purchase tbody tr").each(function (i, item) {
                    var _data = $(item).data('data');
                    if (_data.data.mtid == data.mtid) {

                        exist = true;
                        _data.num++;
                        $(item).data('data', _data);
                        app.purchase.refresh(item);
                    }
                })
                if (exist) {
                    return;
                }
                if (data.price == null) {
                    data.price = 0;
                }
                if (data.selling_price == null) {
                    data.selling_price = 0;
                }
                var tr = $("<tr></tr>");
                var tplHtml = "";
                tplHtml += "<td class='center'>" + data.name + "</td>";
                tplHtml += "<td class='center'><span title='" + data.brand + '\n' + data.description + "'>" + data.type + "</span></td>";
                tplHtml += "<td class='center'>" + data.format + "</td>";
                tplHtml += "<td class='center'><input id='price_" + index + "' name='price' type='text' style='width:50px; text-align:right' value='" + data.price + "' " +
                    "onfocus='app.purchase.fous(this.id,"+data.price+")' onblur='app.purchase.onbr("+data.price+",this.id,this)' onkeyup='app.purchase.price(this)' ></td>";
                if($("#set_selling_price").prop("checked")){
                    tplHtml += "<td style='display: block' class='selling_price center'>" +
                        "<input id='selling_price_" + index + "' name='selling_price' type='text' style='width:50px; text-align:right' value='" + data.selling_price + "' " +
                        "onfocus='app.purchase.fous(this.id)' onblur='app.purchase.onbr("+data.selling_price+",this.id)' onkeyup='app.purchase.sellingPrice(this)'></td>";
                }else {
                    tplHtml += "<td style='display: none;' class='selling_price center'>" +
                        "<input id='selling_price_" + index + "' name='selling_price' type='text' style='width:50px; text-align:right'  value='" + data.selling_price + "' " +
                        "onfocus='app.purchase.fous(this.id)' onblur='app.purchase.onbr("+data.selling_price+",this.id)'onkeyup='app.purchase.sellingPrice(this)'></td>";
                }

                tplHtml += "<td><div class='mt_num_ctrl' id=''><a class='mt_num_ctrl_dec'   onclick='app.purchase.num(this,\"sub\")'>-</a>";
                tplHtml += "<input id='num_" + index + "' name='quantity' type='text' onblur='app.purchase.checkNum(this.id,this)'  class='mt_num_ctrl_input' value='1'/>";
                tplHtml += "<a class='mt_num_ctrl_add'   onclick='app.purchase.num(this,\"add\")'>+</a></div></td>";

                tplHtml += "<td class='right'><span id='totalPrice_" + index + "'>" + data.price + "</span></td>";
                tplHtml += "<td class='center'> <a class='del' href='javascript:void(0);'  onclick='javascript:app.purchase.del(this);'></a></td>";

                tr.append(tplHtml);
                $(tr).data("data", { data:data, price:data.price, num:1} );
                $("#to_purchase").append(tr);
                total += parseFloat(data.price);
                total = parseFloat(total.toFixed(2));
                $("#total_price").html(total);
            },
            fous:function(id,price){
                var arr = id.split('_');
                var index = arr[1];
                if (arr.length > 2){
                    index = arr[2];
                    $("#selling_price_"+index).val("");
                }else {
                    $("#price_"+index).val("");
                }
            },
            onbr:function(price,id,e){
                var arr = id.split('_');
                var index = arr[1];
                var index = arr[1];
                if (arr.length > 2){
                    index = arr[2];
                    var newsSellingPrice = $("#selling_price_" + index).val();
                    if (newsSellingPrice == '') {
                        $("#selling_price_" + index).val(price);
                    }
                }else {
                    var newPrice = $("#price_" + index).val();
                    if (newPrice == '') {
                        $("#price_" + index).val(price);
                        newPrice = price;
                    }
                    var data = $(e).parents("tr").data('data');
                    data.price = newPrice;
                    $(e).parents("tr").data('data', data);
                    this.refresh($(e).parents("tr"));
                }

            },
            num: function (e, t) {
                var data = $(e).parents("tr").data('data');
                var num = data.num;
                if (t == 'add') {
                    num++;
                }
                if (t == 'sub') {
                    if (num > 1) {
                        num--;
                    }
                }
                data.num = num;
                $(e).parents("tr").data('data', data);
                this.refresh($(e).parents("tr"));
            },
            checkNum: function (obj, e) {
                var data = $(e).parents("tr").data('data');
                var arr = obj.split('_');
                var index = arr[1];
                var num = $("#num_" + index).val();
                if (!(/(^[1-9]\d*$)/.test(num))) {
                    layer.alert("请输入正确的数量格式");
                    $("#num_" + index).val(1);
                    data.num = 1;
                    $(e).parents("tr").data('data', data);
                    this.refresh($(e).parents("tr"));
                    return false;
                }
                data.num = num;
                $(e).parents("tr").data('data', data);
                this.refresh($(e).parents("tr"));
            },
            price: function (e) {
                e.value = e.value.replace(/[^\d.]/g,""); //清除"数字"和"."以外的字符
                e.value = e.value.replace(/^\./g,""); //验证第一个字符是数字而不是
                e.value = e.value.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的
                e.value = e.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
                e.value = e.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); //只能输入两个小数

                var data = $(e).parents("tr").data('data');
                var price = $(e).val();
                if (price == ''){
                    $(e).val("");
                }
                /*if (!(/^\d+(\.{0,1}\d+){0,1}$/.test(price))) {
                 layer.alert("请输入正确的单价格式");
                 $(e).val(0);
                 data.price = 0;
                 $(e).parents("tr").data('data', data);
                 this.refresh($(e).parents("tr"));
                 return false;
                 }*/
                data.price = price;
                $(e).parents("tr").data('data', data);
                this.refresh($(e).parents("tr"));
            },
            sellingPrice: function (e) {
                e.value = e.value.replace(/[^\d.]/g,""); //清除"数字"和"."以外的字符
                e.value = e.value.replace(/^\./g,""); //验证第一个字符是数字而不是
                e.value = e.value.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的
                e.value = e.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
                e.value = e.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); //只能输入两个小数

                var selling_price = $(e).val();
                if (selling_price == ''){
                    $(e).val("");
                }
//                layer.alert("请输入正确的售价价格格式");
                /*if (!(/^\d+(\.{0,1}\d+){0,1}$/.test(selling_price))) {
                 layer.alert("请输入正确的单价格式");
                 $(e).val(0);
                 return false;
                 }*/
            },
            refresh: function (tr) {
                var data = $(tr).data('data');
                total = (data.price * data.num).toFixed(2);
                $(tr).find("td").eq(6).find("span").html(total);
                $(tr).find("td").eq(5).find("input").val(data.num);

                total = 0;
                $("#to_purchase tbody tr").each(function (i, item) {
                    var _data = $(item).data('data');
                    total += parseFloat((_data.price * _data.num).toFixed(2));
                })
                total = parseFloat(total.toFixed(2));
                $("#total_price").html(total);
            },
            del: function (e) {
                $(e).parents("tr").remove();
                total = 0;
                $("#to_purchase tbody tr").each(function (i, item) {
                    var _data = $(item).data('data');
                    total += _data.price * _data.num;
                })
                total = parseFloat(total.toFixed(2));
                $("#total_price").html(total);
            },
        }
    }

    function checkNum()
    {
        var quantity = $("#num_" + index).val();
        if (quantity == '') {
            layer.alert("请输入配件数量");
            return false;
        }
        if (!(/(^[0-9]\d*$)/.test(quantity))) {
            layer.alert("请输入正确的数量格式");
            return false;
        }
    }

    $(function () {
        app.init();
        app.search(1);
    })

</script>

<style type="text/css">
    .disable {
        color: grey;
    }
</style>
<div id="content">
    <div class="box">
        <div class="heading">
            <h1><img src="<?=Url::to('@web/admin/view/image/')?>payment.png" alt=""/>新增入库</h1>

            <div class="buttons">
                <a href="<?=Url::to(['inventory/warehousing_list'])?>" class="button"><span>返回</span></a>
            </div>
        </div>

        <div class="purchase">
            <div class="dashboard-content">
                <div class="query-param">
                    <span class="title">品类</span>

                    <div>
                        <ul id="allCategory">
                            <li><a onclick="app.change.material(this)" href="javascript:void(0);" data-id=""
                                   class="mt_select material-item selected" data-key="material">全部</a></li>
                            <?php if (!empty($materials)): ?>
                                <?php foreach ($materials as $row): ?>
                                    <li><a onclick="app.change.material(this)" href="javascript:void(0);"
                                           data-id="<?=$row['id']?>" class="mt_select material-item "
                                           data-key="material"><?=$row['name']?></a></li>
                                <?php endforeach;
                            endif;?>
                        </ul>
                        <ul id="storeCategory" style="display: none;">
                            <li><a onclick="app.change.material(this)" href="javascript:void(0);" data-id=""
                                   class="mt_select material-item selected" data-key="material">全部</a></li>
                            <?php if (!empty($dealer_material)): ?>
                                <?php foreach ($dealer_material as $row): ?>
                                    <li><a onclick="app.change.material(this)" href="javascript:void(0);"
                                           data-id="<?=$row['id']?>" class="mt_select material-item "
                                           data-key="material"><?=$row['name']?></a></li>
                                <?php endforeach;
                            endif;?>
                        </ul>
                    </div>
                </div>
                <div style="clear: both;"></div>
                <div class="query-param" style="display: none;">
                    <span class="title">品牌</span>

                    <div>
                        <ul id="brand-list">
                        </ul>
                    </div>
                </div>
                <div style="clear: both;"></div>
                <input class="group-key" id="material" name="material" value="" type="hidden">
                <input class="group-key" id="brand" name="brand" type="hidden">

                <HR style=" margin-bottom:15px" width="100%" color=#8EAEC3 SIZE=1>
                <div style="min-height: 36px;width: 100%">
                    <div style="width: 50%;float: left;">
                        <label style="font-weight:bold">型号模糊查询&nbsp;&nbsp;</label>
                        <input type="text" class="group-key" name="key" value="">&nbsp;
                        <a href="javascript:void(0);" onclick="app.search()" class="button">查询</a>
                    </div>
                    <div style="width: 50%;float: left">
                        <label style="font-weight:bold;">只显示本店材料</label>
                        <input id="showStoreCategoryCb" type="checkbox" value="1" onclick="showStoreCategory(this);">
                        <a id="add_material_btn" style="margin-left: 5%;"  onclick="add_material()" class="button">新增材料</a>
                    </div>
                </div>
            </div>
        </div>
        <div class="purchase_select" style="clear: both">
            <div class="dashboard-heading">配件列表</div>
            <div class="dashboard-content" style="height: 655px">
                <div style="height: 600px;overflow: hidden;overflow-y: auto;">
                    <table id="to_select" class="list" style="margin-top: -0.5px">
                        <thead>
                        <tr>
                            <td class="left">品类</td>
                            <td class="left">品牌</td>
                            <td class="left">型号</td>
                            <td class="center">操作</td>
                        </tr>
                        </thead>
                        <tbody>

                        </tbody>
                    </table>
                </div>
                <div class="" id="pagination" style="padding-top: 15px"></div>
            </div>
        </div>
        <div class="purchase_list">
            <div class="dashboard-heading">入库列表</div>
            <div id="table_list_div" class="dashboard-content" style="height: 650px;padding: 5px 8px 10px 8px">
                <div style="height: 400px;">
                    <table id="to_purchase" class="list">
                        <thead>
                        <tr style="background-color: #EFEFEF">
                            <th class="center">品类</th>
                            <th class="center">型号</th>
                            <th class="center">单位</th>
                            <th class="center">进价</th>
                            <th class="selling_price center">售价</th>
                            <th class="center">数量</th>
                            <th class="center">价格</th>
                            <th class="td_hide">隐藏</th>
                            <th class="center">操作</th>
                        </tr>
                        </thead>
                        <tbody>

                        </tbody>
                    </table>
                </div>
                <span class="price">金额总计：￥<span id="total_price" style="margin-right: 30px">0</span></span><br/>
                <div>
                    <span style="vertical-align:middle;">入库类型：</span>
                    <select id="operation" m='search' onchange="operationChange(this)" name="car_1" style="width:200px; height:30px;vertical-align:middle;">
                        <option value="1" selected="selected">采购入库</option>
                        <option value="3">其他入库</option>
                    </select>
                    <span style=" margin-left:10px;"><input id="set_selling_price" type="checkbox" value="1" onchange="check_change_selling_price();"><label>设置售价</label></span>
                </div>

                <div style="margin-top:10px">
                    <span style="">入库时间：</span>
                    <input style="width:200px; height:30px" class="date" id="create_date"
                           value="<?=date('Y-m-d H:i')?>" type="text" readonly>
                </div>

                <div style="margin-top:10px;width:100%;">
                    <span style="float:left;margin-top: 5px">操作人员：</span>
                <span>
                    <select style="width:100px; height:29px; float:left;margin-right:20px;margin-left:5px;" id="jobs"
                            onchange="javascript:getWorkers(this)">
                        <?php foreach($workers_job as $value): ?>
                            <?php if($value['job'] == $account['job']): ?>
                                <option value="<?=$value['job']?>"  selected="selected"><?=$value['job']?></option>
                            <?php else: ?>
                                <option value="<?=$value['job']?>"><?=$value['job']?></option>
                            <?php endif; ?>
                        <?php endforeach;?>
                    </select>&nbsp;&nbsp;&nbsp;&nbsp;
                <ul id="workers" style="font-size:14px; font-weight:100; display:inline;">
                    <?php foreach($workers as $worker): ?>
                        <li id="<?=$worker['job']?>" style="float:left; margin:5px 20px 10px 0px;">
                            <?php if($worker['name'] == $account['name']): ?>
                                <input type="checkbox" class="worker" name="<?=$worker['job']?>"
                                       value="<?=$worker['name']?>" checked="checked"> <?=$worker['name']?>
                            <?php else: ?>
                                <input type="checkbox" class="worker" name="<?=$worker['job']?>"
                                       value="<?=$worker['name']?>"> <?=$worker['name']?>
                            <?php endif; ?>
                        </li>
                    <?php endforeach;?>
                </ul>
                </span>
                </div>
                <div>
                <textarea style="margin-top:10px; width:99%; height:60px; margin-bottom:15px;resize: none;" id="extra_requirement"
                          name="extra_requirement" placeholder="补充说明:"></textarea>
                    <form action="<?=Url::to(['inventory/purc_enter_warehouse'])?>" method="post"
                          enctype="multipart/form-data" id="form">
                        <input type="hidden" id="purchase" name="purchase">
                    </form>
                </div>
                    <a href="javascript:void(0);" onclick="app.clearing();" id="btn-submit" class="button">提交入库</a>
            </div>
        </div>

    </div>
</div>
<script>
    function showStoreCategory(obj)
    {
        var isChecked = $("#showStoreCategoryCb").is(':checked');
        if (isChecked) {
            $("#allCategory").hide();
            $("#storeCategory").show();
            $("#storeCategory li a").removeClass("selected");
            $("#storeCategory li:nth-child(1) a").addClass("selected");
            $("#material").val("");
            $("#brand").val("");
            app.change.loadBrand(0);

        }
        else {
            $("#allCategory").show();
            $("#storeCategory").hide();
            $("#allCategory li a").removeClass("selected");
            $("#allCategory li:nth-child(1) a").addClass("selected");
            $("#material").val("");
            $("#brand").val("");
            app.change.loadBrand(0);
        }
    }

    function add_material()
    {
        layer.open({
            type: 2,
            title: "<span style='font-weight: bold;font-size: 14px'>新增材料</span>",
            skin: "layui-layer-lan",
            shadeClose: false,
            area: ['50%', '70%'],
            content: '<?=Url::to(['inventory/addMaterialTypeLyaer'])?>'
        });
    }

    $("#choose_dealer").live('change', function (e) {
        var selected = $("#choose_dealer").val();
        if (selected == "addDealer") {
            add(e);
        }
    });

    function updateMaterialList(data)
    {
        if ($(".add_new_material").length == 0) {
            $("#to_select tr:gt(0)").each(function () {
                $(this).parent().remove();
            });
            $("#allCategory li a").removeClass("selected");
            $("#storeCategory li a").removeClass("selected");
            $("#allCategory li:nth-child(1) a").addClass("selected");
            $("#storeCategory li:nth-child(1) a").addClass("selected");
            $("#brand-list li a").removeClass("selected");
            $("#brand-list li:nth-child(1) a").addClass("selected");
            $("#material").val("");
            $("#brand").val("");

            $("#pagination").html("");
        }
        var table = $("#to_select");
        var products = data;
        var row = $("<tr class='add_new_material'></trclass>");
        var td = $("<td class='left'>" + products.name + "</td>" +
            "<td class='left'>" + products.brand + "</td>" +
            "<td class='left' title='" + products.description + "'>" + products.type + "</td>" +
            "<td class='center'> <a class='add' href='javascript:void(0);' " +
            "name='" + products.name + "_" + products.id + "_" + products.type + "_" + products.sale_price + "_" + products.mid +
            "_" + products.mtid + "_" + products.dealer_id + "_" + products.format + "' onclick='javascript:app.purchase.add(this);' " +
            "data-brand='" + products.brand + "'></a> </td>");
        $(row).data('data', products);
        row.append(td);
        table.append(row);
    }

    function operationChange(){
        var obj = arguments[0] ? arguments[0] : '';
        if($(obj).val() == 7){
            $('#dealerDiv').css('display','none');
            $('#storeDiv').css('display','block');
            $('#btn-submit').text('提交调拨');
        }else{
            $('#dealerDiv').css('display','block');
            $('#storeDiv').css('display','none');
            $('#btn-submit').text('提交入库');
        }
    }

    function check_change_selling_price()
    {
        if ($("#set_selling_price").prop("checked"))
            $(".selling_price").show();
        else
            $(".selling_price").hide();
    }

    !function (app) {
        $.fn[app] = {
            add: function (action, id, fn) {
                layer.open({
                    type: 2,
                    title: "<span style='font-weight: bold;font-size: 14px'>新增供应商</span>",
                    shadeClose: false,
                    btn: ['保存', '取消'], yes: function (index, layero) { //或者使用btn1
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        iframeWin.save();
                    },
                    area: ['700px', '80%'],
                    content: '<?=Url::to(['dealer/popub_edit'])?>?action=' + action + "&id=" + id,
                    success: function (layero, index) {
                        var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        iframeWin.callBack = fn;
                    }
                });
            }
        }
    }('dealer');

    function add(e) {
        $(e).dealer.add("add", "", function (d) {
            refreshDealer();
        });
    }

    function refreshDealer() {
        var data = null;
        $.post("<?=Url::to(['inventory/refreshDealer'])?>", {data:data}, function (d) {
            if (d.flag) {
                var ht = '<option value="">请选择供应商</option>' + '<option value="addDealer">新增</option>';
                var len = d.data.length;
                for (var i = 0; i < len; i++) {
                    ht += "<option value='" + d.data[i]['id'] + "'>" + d.data[i]['name'] + "</option>";
                }
                $('#choose_dealer').html(ht);
            }
            else {
                layer.alert("请求失败");
            }
        }, "json");
    }

    $("#others").click(function () {
        if ($("#others").is(':checked')) {
            $("#choose_dealer").val("");
            $("#choose_dealer").attr("disabled", true);
        }
        else {
            $("#choose_dealer").attr("disabled", false);
        }
    });

    //显示操作人员
    getWorkers();
    function getWorkers() {
        var jobs = $("#jobs").val();
        $("#workers li").each(function () {
            $(this).hide();
            if ($(this).attr('id') == jobs)
                $(this).show();
        });
    }

    //select模糊匹配
    $(document).ready(function () {
        $('#operation').selectseach();
//        $('#choose_dealer').selectseach();
//        $('#choose_store').selectseach();
    });
</script>